package com.beverages.messages.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.messages.client.*;
import com.beverages.messages.entity.RefundRequest;
import com.beverages.messages.entity.dto.RefundRequestDTO;
import com.beverages.messages.entity.dto.intermediate.OrdersDTO;
import com.beverages.messages.entity.intermediate.Commodity;
import com.beverages.messages.entity.intermediate.RefundReply;
import com.beverages.messages.mapper.RefundRequestMapper;
import com.beverages.messages.service.IRefundRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class RefundRequestServiceImpl extends ServiceImpl<RefundRequestMapper, RefundRequest> implements IRefundRequestService {

    @Autowired
    private CustomerCommoditiesClient customerCommoditiesClient;

    @Autowired
    private CustomerIndividualClient customerIndividualClient;

    @Autowired
    private CustomerOrdersClient customerOrdersClient;

    @Autowired
    private SharedFilesClient sharedFilesClient;

    @Autowired
    private MerchantMessagesClient merchantMessagesClient;

    @Override
    public RefundRequestDTO convertRefundRequestEntityToRefundRequestDTO(RefundRequest refundRequest) {
        RefundRequestDTO dto = new RefundRequestDTO();

        dto.setRequestId(refundRequest.getId());
        dto.setOrderNumber(refundRequest.getOrderNumber());
        dto.setRequestTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refundRequest.getRequestTime()));

        OrdersDTO oneOrderDTO = customerOrdersClient.findOneByOrderNumber(refundRequest.getOrderNumber()).getData();
        dto.setMilkteaId(oneOrderDTO.getMilkteaId());
        dto.setMilkteaName(oneOrderDTO.getMilkteaName());
        dto.setFromCustomerId(oneOrderDTO.getCustomerId());
        dto.setFromCustomerName(oneOrderDTO.getCustomerName());
        dto.setFromCustomerPhone(oneOrderDTO.getCustomerPhone());

        dto.setRequestContent(refundRequest.getContent());

        dto.setImageUrl(sharedFilesClient.getFileUrlById(refundRequest.getImageId()).getData());

        dto.setReplyId(refundRequest.getReplyId());
        if(! refundRequest.getReplyId().equals(0)){
            RefundReply refundReply = merchantMessagesClient.getRefundReplyById(refundRequest.getReplyId()).getData();
            dto.setReplyContent(refundReply.getContent());
            dto.setDecision(refundReply.getDecision());
            dto.setAmount(refundReply.getAmount());
            dto.setReplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(refundReply.getReplyTime()));
        }

        return dto;
    }


}
